﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AppMain
{
    public partial class TxSwitcher : Form
    {
        //public delegate void CallbackFunction();
        RadioButton[] _antenna = new RadioButton[SwMatrix.NumberOfAntennas];

        public PreviewSwitchCfg _previewPane;

        public TxSwitcher()
        {
            InitializeComponent();

            // Create Radio Buttons
            for (byte x = 0; x < SwMatrix.NumberOfAntennas; x++)
            {
                _antenna[x] = new RadioButton();
                _antenna[x].Text = SwMatrix._antennaNames[x];
                _antenna[x].Location = new System.Drawing.Point(165, 11 + 50*x);
                _antenna[x].Click += new System.EventHandler(Antenna_Click);
                this.Controls.Add(_antenna[x]);
            }

            // Make sure that the correct Antenna is selected by default
            UpdateRadioButtons();
        }

        private void UpdateTimer_Tick(object sender, EventArgs e)
        {
            UpdateDisplay();
        }
        
        private void UpdateSelectionLogic()
        {
            byte oldConnectedTransmitter = 0;
            byte antennaSelection = 0;
            for (byte x = 0; x < SwMatrix.NumberOfAntennas; x++)
            {
                if(_antenna[x].Checked)
                {
                    antennaSelection += x;
                }
            }
            // Make sure that this Antenna is not already used.
            // If it is, it will be necessary to flip the transmitters
            for (byte x = 1; x <= SwMatrix.MaximumTransmitterNumber; x++)
            {
                if (SwMatrix._antennaSelected[x] == antennaSelection)
                {
                    oldConnectedTransmitter = x;
                }
            }
            if ((oldConnectedTransmitter > 0) && (oldConnectedTransmitter != SwMatrix._txSelected))
            {
                byte oldAntennaSelection = SwMatrix._antennaSelected[SwMatrix._txSelected];
                SwMatrix._antennaSelected[oldConnectedTransmitter] = oldAntennaSelection;
            }
            SwMatrix._antennaSelected[SwMatrix._txSelected] = antennaSelection;
        }

        private void UpdateDisplay()
        {
            bool antennaConnected = false;
            Transmitter1.BackColor = SwMatrix._txColor[1];
            Transmitter2.BackColor = SwMatrix._txColor[2];
            Transmitter5.BackColor = SwMatrix._txColor[5];
            Transmitter6.BackColor = SwMatrix._txColor[6];
            Transmitter7.BackColor = SwMatrix._txColor[7];
            for (byte y = 0; y < SwMatrix.NumberOfAntennas; y++)
            {
                antennaConnected = false;
                for (byte x = 1; x <= SwMatrix.MaximumTransmitterNumber; x++)
                {
                    if (SwMatrix._antennaSelected[x] == y)
                    {
                        antennaConnected = true;
                        _antenna[y].ForeColor = SwMatrix._txColor[x];
                    }
                }
                if (!antennaConnected)
                {
                    _antenna[y].ForeColor = SwMatrix._txColor[0];
                }
            }
        }

        private void UpdateRadioButtons()
        {
            for (byte y = 0; y < SwMatrix.NumberOfAntennas; y++)
            {
                if (SwMatrix._antennaSelected[SwMatrix._txSelected] == y)
                {
                    _antenna[y].Checked = true;
                }
                else
                {
                    //_antenna[y].Checked = false;
                }
            }
        }

        private void Transmitter6_Click(object sender, EventArgs e)
        {
            SwMatrix._txSelected = 6;
            UpdateRadioButtons();
        }

        private void Transmitter2_Click(object sender, EventArgs e)
        {
            SwMatrix._txSelected = 2;
            UpdateRadioButtons();
        }

        private void Transmitter1_Click(object sender, EventArgs e)
        {
            SwMatrix._txSelected = 1;
            UpdateRadioButtons();
        }

        private void Transmitter7_Click(object sender, EventArgs e)
        {
            SwMatrix._txSelected = 7;
            UpdateRadioButtons();
        }

        private void Transmitter5_Click(object sender, EventArgs e)
        {
            SwMatrix._txSelected = 5;
            UpdateRadioButtons();
        }

        private void LockTX6_CheckedChanged(object sender, EventArgs e)
        {
            SwMatrix._transmitterLocked[6] = LockTX6.Checked;
            if (_previewPane != null)
            {
                if (_previewPane.Visible)
                {
                    byte alternates = SwMatrix.DetermineOptimalSwitchConfiguration();
                    if (alternates > 0)
                    {
                        bool valid = SwMatrix.UpdateWorkingSwitchConfigWithAlternate(0);
                        SwMatrix._lastAlternate = 0;
                        byte[] workingTargetSwitchPositions = new byte[11];
                        for (byte x = 0; x < 11; x++)
                        {
                            if (SwMatrix._workingTargetSwitchPositions[x])
                            {
                                workingTargetSwitchPositions[x] = 1;
                            }
                            else
                            {
                                workingTargetSwitchPositions[x] = 0;
                            }

                        }
                        SwMatrix.CalculateTransmitterFeeds(workingTargetSwitchPositions);
                    }
                    else
                    {
                        SwMatrix.CalculateTransmitterFeeds(SwMatrix._switchPositions);
                    }

                    _previewPane.RedisplayRadioButtons(alternates);
                }
            }
        }

        private void LockTX2_CheckedChanged(object sender, EventArgs e)
        {
            SwMatrix._transmitterLocked[2] = LockTX2.Checked;
            if (_previewPane != null)
            {
                if (_previewPane.Visible)
                {
                    byte alternates = SwMatrix.DetermineOptimalSwitchConfiguration();
                    if (alternates > 0)
                    {
                        bool valid = SwMatrix.UpdateWorkingSwitchConfigWithAlternate(0);
                        SwMatrix._lastAlternate = 0;
                        byte[] workingTargetSwitchPositions = new byte[11];
                        for (byte x = 0; x < 11; x++)
                        {
                            if (SwMatrix._workingTargetSwitchPositions[x])
                            {
                                workingTargetSwitchPositions[x] = 1;
                            }
                            else
                            {
                                workingTargetSwitchPositions[x] = 0;
                            }

                        }
                        SwMatrix.CalculateTransmitterFeeds(workingTargetSwitchPositions);
                    }
                    else
                    {
                        SwMatrix.CalculateTransmitterFeeds(SwMatrix._switchPositions);
                    }

                    _previewPane.RedisplayRadioButtons(alternates);
                }
            }
        }

        private void LockTX1_CheckedChanged(object sender, EventArgs e)
        {
            SwMatrix._transmitterLocked[1] = LockTX1.Checked;
            if (_previewPane != null)
            {
                if (_previewPane.Visible)
                {
                    byte alternates = SwMatrix.DetermineOptimalSwitchConfiguration();
                    if (alternates > 0)
                    {
                        bool valid = SwMatrix.UpdateWorkingSwitchConfigWithAlternate(0);
                        SwMatrix._lastAlternate = 0;
                        byte[] workingTargetSwitchPositions = new byte[11];
                        for (byte x = 0; x < 11; x++)
                        {
                            if (SwMatrix._workingTargetSwitchPositions[x])
                            {
                                workingTargetSwitchPositions[x] = 1;
                            }
                            else
                            {
                                workingTargetSwitchPositions[x] = 0;
                            }

                        }
                        SwMatrix.CalculateTransmitterFeeds(workingTargetSwitchPositions);
                    }
                    else
                    {
                        SwMatrix.CalculateTransmitterFeeds(SwMatrix._switchPositions);
                    }

                    _previewPane.RedisplayRadioButtons(alternates);
                }
            }
        }

        private void LockTX7_CheckedChanged(object sender, EventArgs e)
        {
            SwMatrix._transmitterLocked[7] = LockTX7.Checked;
            if (_previewPane != null)
            {
                if (_previewPane.Visible)
                {
                    byte alternates = SwMatrix.DetermineOptimalSwitchConfiguration();
                    if (alternates > 0)
                    {
                        bool valid = SwMatrix.UpdateWorkingSwitchConfigWithAlternate(0);
                        SwMatrix._lastAlternate = 0;
                        byte[] workingTargetSwitchPositions = new byte[11];
                        for (byte x = 0; x < 11; x++)
                        {
                            if (SwMatrix._workingTargetSwitchPositions[x])
                            {
                                workingTargetSwitchPositions[x] = 1;
                            }
                            else
                            {
                                workingTargetSwitchPositions[x] = 0;
                            }

                        }
                        SwMatrix.CalculateTransmitterFeeds(workingTargetSwitchPositions);
                    }
                    else
                    {
                        SwMatrix.CalculateTransmitterFeeds(SwMatrix._switchPositions);
                    }

                    _previewPane.RedisplayRadioButtons(alternates);
                }
            }
        }

        private void LockTX5_CheckedChanged(object sender, EventArgs e)
        {
            SwMatrix._transmitterLocked[5] = LockTX5.Checked;
            if (_previewPane != null)
            {
                if (_previewPane.Visible)
                {
                    byte alternates = SwMatrix.DetermineOptimalSwitchConfiguration();
                    if (alternates > 0)
                    {
                        bool valid = SwMatrix.UpdateWorkingSwitchConfigWithAlternate(0);
                        SwMatrix._lastAlternate = 0;
                        byte[] workingTargetSwitchPositions = new byte[11];
                        for (byte x = 0; x < 11; x++)
                        {
                            if (SwMatrix._workingTargetSwitchPositions[x])
                            {
                                workingTargetSwitchPositions[x] = 1;
                            }
                            else
                            {
                                workingTargetSwitchPositions[x] = 0;
                            }

                        }
                        SwMatrix.CalculateTransmitterFeeds(workingTargetSwitchPositions);
                    }
                    else
                    {
                        SwMatrix.CalculateTransmitterFeeds(SwMatrix._switchPositions);
                    }

                    _previewPane.RedisplayRadioButtons(alternates);
                }
            }
        }

        private void Antenna_Click(object sender, EventArgs e)
        {
            UpdateSelectionLogic();
            // Clear Alternate selection from Before
            SwMatrix._lastAlternate = 0;
            if (_previewPane != null)
            {
                if (_previewPane.Visible)
                {
                    byte alternates = SwMatrix.DetermineOptimalSwitchConfiguration();
                    if (alternates > 0)
                    {
                        bool valid = SwMatrix.UpdateWorkingSwitchConfigWithAlternate(0);
                        SwMatrix._lastAlternate = 0;
                        byte[] workingTargetSwitchPositions = new byte[11];
                        for (byte x = 0; x < 11; x++)
                        {
                            if (SwMatrix._workingTargetSwitchPositions[x])
                            {
                                workingTargetSwitchPositions[x] = 1;
                            }
                            else
                            {
                                workingTargetSwitchPositions[x] = 0;
                            }

                        }
                        SwMatrix.CalculateTransmitterFeeds(workingTargetSwitchPositions);
                    }
                    else
                    {
                        SwMatrix.CalculateTransmitterFeeds(SwMatrix._switchPositions);
                    }

                    _previewPane.RedisplayRadioButtons(alternates);
                }
            }
        }

        private void PreviewConfig_Click(object sender, EventArgs e)
        {
            byte alternates = SwMatrix.DetermineOptimalSwitchConfiguration();
            if (alternates > 0)
            {
                bool valid = SwMatrix.UpdateWorkingSwitchConfigWithAlternate(0);
                SwMatrix._lastAlternate = 0;
                byte[] workingTargetSwitchPositions = new byte[11];
                for (byte x = 0; x < 11; x++)
                {
                    if (SwMatrix._workingTargetSwitchPositions[x])
                    {
                        workingTargetSwitchPositions[x] = 1;
                    }
                    else
                    {
                        workingTargetSwitchPositions[x] = 0;
                    }

                }
                SwMatrix.CalculateTransmitterFeeds(workingTargetSwitchPositions);
            }
            else
            {
                SwMatrix.CalculateTransmitterFeeds(SwMatrix._switchPositions);
            }
            if (_previewPane != null)
            {
                if (!_previewPane.Visible)
                {
                    _previewPane = new PreviewSwitchCfg(alternates);
                }
            }
            else
            {
                _previewPane = new PreviewSwitchCfg(alternates);
            }
            _previewPane.Show();

        }

        private void CommitConfig_Click(object sender, EventArgs e)
        {
            byte alternates = SwMatrix.DetermineOptimalSwitchConfiguration();
            if (alternates > 0)
            {
                SwMatrix.UpdateWorkingSwitchConfigWithAlternate(SwMatrix._lastAlternate);
                for (byte x = 0; x < 11; x++)
                {
                    SwMatrix._targetSwitchPositions[x] = SwMatrix._workingTargetSwitchPositions[x];
                }
                if (_previewPane != null)
                {
                    _previewPane.Close();
                }
            }
            this.Close();
        }
    }
}

